/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecompuefs.micc.lfs.slave;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author emanuel
 */
public class Slave {

    private static String getHostAddress() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    private static String masterIP;
    private static int masterPort;
    private static List<String> file;
    private static Socket client;
    
    /**
	 * @param args
	 */
	public static void main(String[] args) {
            
            // create, if needed, the root directory of the file system
		File rootDir = new File("sd:/");
		if (!rootDir.exists()) {
			rootDir.mkdir();
		}

		file = new ArrayList<String>();
		loadFiles(rootDir);
		file.add("sd:/");

		String hostname = getHostAddress();
                
                try {

            while (masterIP == null || masterIP.equals("")) {

                masterIP = JOptionPane.showInputDialog("Digite o Endereço do Servidor Master");
                System.out.println(masterIP);

                if (masterIP == null || masterIP.equals("")) {

                    JOptionPane.showMessageDialog(null,
                            "Você não digitou um endereço válido.");

                }

            }

            client = new Socket(masterIP, 4010);
            
        } catch (UnknownHostException ex) {
            Logger.getLogger(Slave.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Slave.class.getName()).log(Level.SEVERE, null, ex);
        }
            
        }
        
        private static void loadFiles(File dir) {
		String[] files = dir.list();
		for (int i = 0; i < files.length; i++) {
			File f = new File(dir.getAbsolutePath() + "/" + files[i]);
			if (f.isDirectory()) {
				loadFiles(f);
				int index = dir.getAbsolutePath().indexOf("sd:");
				String rootPath = dir.getAbsolutePath().substring(index);
				file.add(rootPath + "/" + files[i] + "/");
				System.out.println(rootPath + "/" + files[i] + "/");
			} else {
				int index = dir.getAbsolutePath().indexOf("sd:");
				String rootPath = dir.getAbsolutePath().substring(index);
				file.add(rootPath + "/" + files[i]);
				System.out.println(rootPath + "/" + files[i]);
			}
		}
	}
    
}
